import typing
from datetime import datetime

from fastapi import APIRouter, Query, Path
from pydantic import Field

import controller.activity
import controller.activity_register
import schemas
from components.router import CenterRoute
from controller.crud_es_query import curd_get_register_records_by_activity, curd_get_register_records_by_person, \
    crud_institution_info, cur_activity_by_institution, cur_activity_by_alumni, crud_register_agg_by_act_type, \
    curd_alumni_agg_by_activity

router = APIRouter(route_class=CenterRoute)


@router.post("/activity/search", response_model=schemas.SearchResult, response_model_exclude_unset=True)
async def search_activities(param: schemas.SearchParam):
    print("我被执行了--2")
    return await controller.activity.search_activity_by_param(param)


@router.post("/desk/activity/register_records", response_model=schemas.SearchResult, response_model_exclude_unset=True)
async def list_register_records(params: schemas.SearchParam):

    if 'person_id' in params.filter:  # 根据人获得获得列表
        return await curd_get_register_records_by_activity(params)

    if 'activity_id' in params.filter:  # 根据获得获得人列表
        return await curd_get_register_records_by_person(params)


@router.get("/desk/alumni/{person_id}/by-year")
async def get_alumni_by_year(person_id, year: int = Query(None, ge=1990, le=2080)):
    """
    人才参与活动数量
    """
    year = year if year else datetime.utcnow().year
    return await curd_alumni_agg_by_activity(person_id, year)


@router.get("/desk/institutions/{institution_id}")
async def get_institution_activity(institution_id, by_year: int = Query(None, ge=1990, le=2080)):
    """
    基本信息+聚类结果
    """
    year = int(by_year) or datetime.utcnow().year

    institution_info = await crud_institution_info(institution_id, year)

    # # 计算不同纬度的指标
    activity_by_institution_agg = await cur_activity_by_institution(institution_id, year)
    activity_by_person_agg = await cur_activity_by_alumni(institution_id, year)
    # # 活动类型
    register_agg_act_type = await crud_register_agg_by_act_type(institution_id, year)

    return {"total": 0, "aggregation": {
        "institution_info": institution_info,
        "activity_by_institution_agg": activity_by_institution_agg,
        "register_agg_act_type": register_agg_act_type,
        "activity_by_person_agg": activity_by_person_agg
    }, "items": None}


@router.get("/desk/institutions/{institution_id}/alumni")
async def get_institution_alumni(institution_id):
    activity_by_institution = await cur_activity_by_alumni(institution_id)
